﻿Public Class frmPaciente

    Public Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Integer, ByVal bRevert As Integer) As Integer
    Public Declare Function RemoveMenu Lib "user32" (ByVal hMenu As Integer, ByVal nPosition As Integer, ByVal wFlags As Integer) As Integer
    Public Const SC_CLOSE = &HF060&
    Public Const MF_BYCOMMAND = &H0&
    Function RemoveXButton(ByVal iHWND As Integer) As Integer
        Dim iSysMenu As Integer
        iSysMenu = GetSystemMenu(iHWND, False)
        Return RemoveMenu(iSysMenu, SC_CLOSE, MF_BYCOMMAND)
    End Function

    Private Sub frmPaciente_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        frmpatient = False
    End Sub

    Private Sub frmPaciente_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Deshabilitar boton [X]
        RemoveXButton(Me.Handle().ToInt32())
        If cP.mtdCargarTipoSeguro.Rows.Count > 0 Then
            With cmbseguro
                .DataSource = cP.mtdCargarTipoSeguro
                .ValueMember = cP.mtdCargarTipoSeguro.Columns(0).ToString
                .DisplayMember = cP.mtdCargarTipoSeguro.Columns(0).ToString
                .SelectedItem = 0
            End With
        End If
        frmpatient = True
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnañadir.Click
        cP.dni_Paciente = txtdni.Text
        cP.nombre_Paciente = txtnombres.Text
        cP.apepaterno_Paciente = txtapellidopaterno.Text
        cP.apematerno_Paciente = txtapellidomaterno.Text
        cP.fecnacimiento_Paciente = dtpfechanacimiento.Text
        cP.sexo_Paciente = cmbsexo.SelectedIndex
        cP.PacienteTipo_codigo_PacienteTipo = cmbseguro.SelectedIndex + 1
        cP.mtdAñadirPaciente()
        cP.mtdBuscarPaciente()
        cP.tipo_PacienteGestante = cmbgestante.SelectedIndex
        If cmbsexo.SelectedIndex = 0 Then
            If (cP.mtdAñadirPacienteGestante() = False) Then    'lo añade a la tabla de pacientegestante donde se guardan a todas las mujeres que estan o no embarazadas              
                cP.mtdActualizarPacienteGestante()              '
            End If
        End If
        LimpiarCajas()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btncerrar.Click
        Me.Close()
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnbuscar.Click

        btneditar.Enabled = True
        btnañadir.Enabled = False
        'txtdni.ReadOnly = True
        txtemail.ReadOnly = False
        txtdireccion.ReadOnly = False
        txtfijo.ReadOnly = False
        txtmovil.ReadOnly = False

        cP.dni_Paciente = txtdnibusqueda.Text

        If (cP.mtdBuscarPaciente()) Then

            txtdni.Text = cP.dni_Paciente
            txtnombres.Text = cP.nombre_Paciente
            txtapellidopaterno.Text = cP.apepaterno_Paciente
            txtapellidomaterno.Text = cP.apematerno_Paciente
            dtpfechanacimiento.Text = cP.fecnacimiento_Paciente

            'Cargar Combo de Sexo del Paciente
            If (cP.sexo_Paciente = 0) Then
                cmbsexo.SelectedIndex = 0
            Else
                cmbsexo.SelectedIndex = 1
            End If

            'Cargar Tipo de Seguro del Paciente
            If cP.PacienteTipo_codigo_PacienteTipo = 1 Then
                cmbseguro.SelectedIndex = 0
            ElseIf cP.PacienteTipo_codigo_PacienteTipo = 2 Then
                cmbseguro.SelectedIndex = 1
            ElseIf cP.PacienteTipo_codigo_PacienteTipo = 3 Then
                cmbseguro.SelectedIndex = 2
            End If

            'Cargar si esta Gestante o No
            If cP.tipo_PacienteGestante = 0 Then
                cmbgestante.SelectedIndex = 0
            Else
                cmbgestante.SelectedIndex = 1
            End If

            txtemail.Text = cP.email_Paciente
            txtdireccion.Text = cP.direccion_Paciente
            txtmovil.Text = cP.telmovil_Paciente
            txtfijo.Text = cP.telfijo_Paciente
            txtdnibusqueda.Text = ""
        End If
        txtdnibusqueda.Text = ""
    End Sub

    Private Sub btneditar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btneditar.Click

        cP.dni_Paciente = txtdni.Text
        cP.nombre_Paciente = txtnombres.Text
        cP.apepaterno_Paciente = txtapellidopaterno.Text
        cP.apematerno_Paciente = txtapellidomaterno.Text
        cP.fecnacimiento_Paciente = dtpfechanacimiento.Text
        cP.sexo_Paciente = cmbsexo.SelectedIndex
        cP.PacienteTipo_codigo_PacienteTipo = cmbseguro.SelectedIndex + 1
        cP.email_Paciente = txtemail.Text
        cP.direccion_Paciente = txtdireccion.Text
        cP.telfijo_Paciente = txtfijo.Text
        cP.telmovil_Paciente = txtmovil.Text
        cP.tipo_PacienteGestante = cmbgestante.SelectedIndex
        cP.mtdActualizarPaciente()
        cP.mtdBuscarPaciente()
        If cmbsexo.SelectedIndex = 0 Then
            cP.tipo_PacienteGestante = cmbgestante.SelectedIndex
            cP.mtdActualizarPacienteGestante()             '
        End If
        btneditar.Enabled = False
        btnañadir.Enabled = True
        LimpiarCajas()
    End Sub

    Private Sub LimpiarCajas()
        txtdni.Text = ""
        txtnombres.Text = ""
        txtapellidomaterno.Text = ""
        txtapellidopaterno.Text = ""
        dtpfechanacimiento.Value = Now
        cmbsexo.SelectedIndex = 0
        cmbseguro.SelectedIndex = 0
        txtemail.Text = ""
        txtdireccion.Text = ""
        txtmovil.Text = ""
        txtfijo.Text = ""
        cmbgestante.SelectedIndex = 0
    End Sub

    Private Sub cmbsexo_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbsexo.SelectedIndexChanged
        If cmbsexo.SelectedIndex = 0 Then
            cmbgestante.Enabled = True
        Else
            cmbgestante.Enabled = False
        End If
    End Sub

    Private Sub txtnombres_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtnombres.KeyPress, txtapellidomaterno.KeyPress, txtapellidopaterno.KeyPress, txtfijo.KeyPress, txtmovil.KeyPress
        'Validacion -> Ingreso de datos SOLO LETRAS
        If Char.IsLetter(e.KeyChar) Then
            e.Handled = False
        ElseIf Char.IsControl(e.KeyChar) Then
            e.Handled = False
            'ElseIf Char.IsSeparator(e.KeyChar) Then
            'e.Handled = False
        Else
            e.Handled = True
        End If
    End Sub
    Private Sub txtdni_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtdni.KeyPress, txtdnibusqueda.KeyPress
        'Validacion -> Ingreso de datos SOLO NUMEROS
        If Char.IsDigit(e.KeyChar) Then
            e.Handled = False
        ElseIf Char.IsControl(e.KeyChar) Then
            e.Handled = False
        Else
            e.Handled = True
        End If
    End Sub

    
    Private Sub frmPaciente_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Leave
        Me.Focus()
    End Sub

    Private Sub frmPaciente_Deactivate(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Deactivate
        Me.Focus()
    End Sub
End Class